package pos.gengshen.com.gspos.activity;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.zxing.other.BeepManager;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import pos.gengshen.com.gspos.R;
import pos.gengshen.com.gspos.common.Constant;
import pos.gengshen.com.gspos.utils.HttpUtils;

/**
 * Created by Administrator on 2018/8/3/003.
 */

public class IntegralActivity extends AppCompatActivity {

    private Button codebtn,scanbtn;
    private EditText codeEdit;
    private TextView userName,integralText;
    private BeepManager mBeepManager;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_integral);
        codebtn = (Button)findViewById(R.id.codebtn);
        scanbtn = (Button) findViewById(R.id.scanbtn);
        codeEdit =(EditText) findViewById(R.id.usercodeEdit);
//        userName = (TextView)findViewById(R.id.username);
//        integralText = (TextView)findViewById(R.id.integralText);
        mBeepManager = new BeepManager(this, R.raw.beep);

        scanbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (checkPackage("com.telpo.tps550.api")) {
                    Intent intent = new Intent();
                    intent.setClassName("com.telpo.tps550.api", "com.telpo.tps550.api.barcode.Capture");
                    try {
                        startActivityForResult(intent, 0x124);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(IntegralActivity.this,"系统错误",
                                Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(IntegralActivity.this, "系统错误2",
                            Toast.LENGTH_LONG).show();
                }
            }
        });

        codebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String code = codeEdit.getText().toString();
                if(code.equals("")){
                    Toast.makeText(IntegralActivity.this,"请输入用户编码",Toast.LENGTH_SHORT).show();
                    return;
                }
                String url = Constant.SERVER_URL+"/admin/member/getUserbyCode?code="+code;
                HttpUtils.doGet(url, null, null, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                        Toast.makeText(IntegralActivity.this,"请输入用户编码",Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        System.out.println(result);
                        //Toast.makeText(IntegralActivity.this,result,Toast.LENGTH_SHORT).show();
                        JSONObject jo = JSON.parseObject(result);
                        String code = jo.getString("code");
                        if(code.equals("200")){
                            JSONObject obj = jo.getJSONObject("obj");
                            String uname = obj.getString("nick");
                            String inteNum = obj.getString("integral");
                            Intent intent = new Intent(IntegralActivity.this,IntegralDetailActivity.class);
                            intent.putExtra("user",obj.toJSONString());
                            startActivity(intent);
                        }
                    }
                });
            }
        });






    }

    private boolean checkPackage(String packageName) {
        PackageManager manager = this.getPackageManager();
        Intent intent = new Intent().setPackage(packageName);
        List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
        if (infos == null || infos.size() < 1) {
            return false;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0x124) {
            if (resultCode == 0) {
                if (data != null) {
                    mBeepManager.playBeepSoundAndVibrate();
                    try {
                        String code = new String(data.getStringExtra("qrCode").getBytes("ISO-8859-1"), "GB2312");
                        //Toast.makeText(MainActivity.this, "扫码结果:" + qrcode, Toast.LENGTH_LONG).show();
                        String url = Constant.SERVER_URL+"/admin/member/getUserbyCode?code="+code;
                        HttpUtils.doGet(url, null, null, new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {

                                Toast.makeText(IntegralActivity.this, "网络错误", Toast.LENGTH_LONG).show();
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                String result = response.body().string();
                                System.out.println(result);
                                //Toast.makeText(IntegralActivity.this,result,Toast.LENGTH_SHORT).show();
                                JSONObject jo = JSON.parseObject(result);
                                String code = jo.getString("code");
                                if(code.equals("200")){
                                    JSONObject obj = jo.getJSONObject("obj");
                                    String uname = obj.getString("nick");
                                    String inteNum = obj.getString("integral");
                                    Intent intent = new Intent(IntegralActivity.this,IntegralDetailActivity.class);
                                    intent.putExtra("user",obj.toJSONString());
                                    startActivity(intent);
                                }
                            }
                        });
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    return;
                }
            } else {
                Toast.makeText(IntegralActivity.this, "Scan Failed", Toast.LENGTH_LONG).show();
            }
        }

    }


}
